package com.jvyou.thread.d09safe1;

/**
 * @author 橘柚
 * @version 1.0-SNAPSHOT
 * @Date 2024/7/31 10:31
 * @Description 同步代码块
 */
public class LockCode extends Thread{
    static int ticket = 0;

    @Override
    public void run() {
        while (true) {
           synchronized (LockCode.class){
               if (ticket < 100) {
                   ticket++;
                   System.out.println(getName() + "卖票：" + ticket);
               } else {
                   break;
               }
           }
        }
    }

    public static void main(String[] args) {
        LockCode t1 = new LockCode();
        t1.setName("窗口1");
        t1.start();
        LockCode t2 = new LockCode();
        t2.setName("窗口2");
        t2.start();
        LockCode t3 = new LockCode();
        t3.setName("窗口3");
        t3.start();
    }
}
